<?php

abstract class Filesystem
{
	/***
 	 * Recurse a directory calling processRecurseDirectory on every file
	 * filter  - list of valid file types  
	 * dirname - directory to recurse
	 */
	abstract function processRecurseDirectory($dirname, $file, $extension);
	final function recurseDirectory($dirname, array $filter = Array()) {
		echo("processing $dirname\n");
		$dir = opendir($dirname);
		while ( ($file = readdir($dir)) != false ) {
			if ( $file == '..' || $file == '.' ) {
				continue;
			}

			if ( is_dir($dirname . '/' . $file) ) {
				self::recurseDirectory($dirname . '/' . $file, $filter);
			} else  {
				$fileinfo = pathinfo($dirname . '/' . $file);

				if ( count($filter) == 0 ) {
					$this->processRecurseDirectory($dirname, $file, $fileinfo['extension']);
				} else if ( in_array($fileinfo['extension'], $filter) ) {
					$this->processRecurseDirectory($dirname, $file, $fileinfo['extension']);
				}
			}
		}
		closedir($dir);
		return;
	}
};

?>
